----------------------------
-- Simplistic Street
-- by Jammie Dodger
--
-- Project Started - October 20, 2009
-- Completion -None
----------------------------

function _R.Player:SaveATM()

	self:SetPData("ATMCash", self:GetATMCash())

end

function _R.Player:LoadATM()

	self:SetNWInt("ATMCash", self:GetPData("ATMCash") or 0)

end

function _R.Player:GetATMCash()

	return self:GetNWInt("ATMCash")

end

function _R.Player:HasATMAmount( Money )

	return (self:GetATMCash() - Money) >= 0
	
end

function _R.Player:TakeATMCash( Money )

	self:SetATMCash( tonumber(self:GetATMCash()) - Money)
	self:SaveATM()
	
end

function _R.Player:GiveATMCash( Money )

	self:SetATMCash( self:GetATMCash() + Money)
	self:SaveATM()
	
end

function _R.Player:SetATMCash( Money )

	self:SetNWInt("ATMCash", Money)
	self:SaveATM()

end

function _R.Player:Interest()

	if ValidEntity(self) and tonumber(self:GetATMCash()) > 0 then
	
	local Amount = math.Round(self:GetATMCash() / 100 * 2.5)
		self:GiveATMCash(Amount)
		self:SendNotify("You have received interest! This has been put into your bank account.", NOTIFY_GENERIC, 5)
	end
end	

function LoadOnSpawn(ply)

	ply:LoadATM()
	timer.Create( ply:UniqueID().."_interest", 911, 0, function() ply:Interest() end)

end

function LooseMoneyOnDeath(victim, weapon, killer)
	if tonumber(victim:GetMoney()) > 500 then
		local Random = math.random(20, 100)
		victim:TakeMoney(Random)
		chat.AddText(victim, Color(154, 205, 50, 255), "[Bank] ", Color(255, 255, 255, 255), "You lost $"..Random.."! Next time, put your money in the bank for safe keeping.")
	end
end

function RemoveOnDisconnect(ply)
	timer.Destroy(ply:UniqueID().."_interest")
end

hook.Add("PlayerDisconnected", "RemoveOnDisconnect", RemoveOnDisconnect)
hook.Add("PlayerInitialSpawn", "LoadOnSpawn", LoadOnSpawn)
hook.Add("PlayerDeath", "LooseMoneyOnDeath", LooseMoneyOnDeath)